<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Events\OpenShop\UserContractCreatedEvent;
use App\Jobs\OpenShop\OrderAutoCloseDelayJob;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;

class OrderAutoCloseListener extends BaseListener
{
    /**
     * 订单延迟关闭，目前是7天
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle(UserContractCreatedEvent $event)
    {
        $contract = OpenShopUserContractModel::find($event->contractId);
        if (null === $contract) {
            return;
        }
        $carbon = $contract->createdAt;
        $carbon->addSeconds(SignOrderEnum::SIGN_DEADLINE_SECONDS);
        // 触发一个延迟队列
        dispatch(new OrderAutoCloseDelayJob($contract->orderNo, $carbon->toDateTimeString()))->onQueue("{listeners}");
    }
}
